package com.cuixichang.minimalism.core.swagger.config;

import com.cuixichang.minimalism.basal.cnstants.HttpHeader;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.Model;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Value("${swagger.enable:true}")
    private String enabed;
    @Value("${swagger.title:}")
    private String title;
    @Value("${swagger.description:在线接口文档说明}")
    private String description;
    @Value("${swagger.email:}")
    private String email;
    @Value("${swagger.version:1.0.0}")
    private String version;
    @Value("${swagger.requestPath:#}")
    private String requestPath;

    @Bean
    @ConditionalOnMissingBean(Docket.class)
    public Docket swaggerSpringMvcPlugin() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(Boolean.valueOf(enabed))
                .ignoredParameterTypes(Model.class, ModelMap.class, BindingResult.class)
                .select()
                //只生成被ApiOperation这个注解注解过的api接口
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                //生成所有API接口
                .paths(PathSelectors.any())
                .build()
                // .useDefaultResponseMessages(false)
                .securitySchemes(securitySchemes())
                .securityContexts(securityContexts())
                ;
    }

    private List<SecurityContext> securityContexts() {
        return Arrays.asList(
                SecurityContext.builder()
                        .securityReferences(defaultAuth())
                        .forPaths(PathSelectors.regex("^(?!login).*$"))
                        .build()
        );
    }

    List<SecurityReference> defaultAuth() {
        List<SecurityReference> result = new ArrayList<>();
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[]{authorizationScope};
        result.add(new SecurityReference(HttpHeader.SYSTEM_SOURCE_CODE, authorizationScopes));
        AuthorizationScope authorizationConditionsScope = new AuthorizationScope("conditions", "authorization protocol");
        AuthorizationScope[] authorizationConditionsScopes = new AuthorizationScope[]{authorizationConditionsScope};
        result.add(new SecurityReference(HttpHeader.AUTHORIZATION, authorizationConditionsScopes));
        return result;
    }


    @SuppressWarnings("unused")
    private Predicate<String> paths() {
        return PathSelectors.regex("/*.*");
    }

    private final List<ApiKey> securitySchemes() {
        return new ArrayList() {{
            add(new ApiKey(HttpHeader.AUTHORIZATION, HttpHeader.AUTHORIZATION, "header"));
            add(new ApiKey(HttpHeader.SYSTEM_SOURCE_CODE, HttpHeader.SYSTEM_SOURCE_CODE, "header"));
        }};
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(title)
                .termsOfServiceUrl(requestPath)
                .description(description)
                .contact(new Contact(title,requestPath,email))
                .version(version)
                .build();
    }
}
